package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.bean.DesignTypeBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产工单列表 适配器
 * Created by tony on 2017/12/12.
 */

public class ProductionOrderListAdapter extends BaseAdapter {

    private Context context;
    private List<DesignTypeBean> datas = new ArrayList<DesignTypeBean>();
    private LayoutInflater inflater;
    private ItemClickListener clickListener;


    public ProductionOrderListAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

    }

    public void setData(List<DesignTypeBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    public void setItemClick(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public DesignTypeBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_production_order_list_lv, null);
            holder.rb = convertView.findViewById(R.id.rb);
            holder.tv_top = convertView.findViewById(R.id.tv_top);
            holder.tv_bottom = convertView.findViewById(R.id.tv_bottom);


            convertView.setTag(holder);
        } else {
            holder = (ProductionOrderListAdapter.ViewHolder) convertView.getTag();
        }

        DesignTypeBean bean = datas.get(position);
        if (bean.isSelected())
            holder.rb.setButtonDrawable(context.getResources().getDrawable(R.mipmap.icon_radio_bg_yes));
        else
            holder.rb.setButtonDrawable(context.getResources().getDrawable(R.mipmap.icon_radio_no));
//        holder.rb.setChecked(bean.isSelected());

        holder.rb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null)
                    clickListener.itemClick(view, position);
            }
        });


        return convertView;
    }

    private class ViewHolder {
        private RadioButton rb;
        private TextView tv_top;
        private TextView tv_bottom;

    }

}
